//
//  WorksListViewController.m
//  MyMusic
//
//  Created by mac on 2019/4/13.
//  Copyright © 2019 YangYang. All rights reserved.
//

#import "WorksListViewController.h"
#import "WorksListTableViewCell.h"
#import "MusicListViewController.h"
#import "MusicPlayingViewController.h"
#import "AlbumModel.h"
#import "UNZipRARService.h"

#import "OtherFileFolderViewController.h"

@interface WorksListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) VLCMediaPlayer *player;


@property (nonatomic, strong) NSString *currentOpenFileTempPath;


@end

@implementation WorksListViewController

- (void)fileNotification:(NSNotification *)notifcation {
    NSDictionary *info = notifcation.userInfo;
    NSString *fileName = [info objectForKey:@"fileName"];
    NSString *filePath = [info objectForKey:@"filePath"];
    
    _currentOpenFileTempPath = filePath;
    
    if ([[[fileName pathExtension] lowercaseString] isEqualToString:@"zip"]) {
        [self unZip:filePath];
    }
    else if ([[[fileName pathExtension] lowercaseString] isEqualToString:@"rar"])
    {
        
    }
    else
    {
        [self handleOtherProjectFile:fileName filePath:filePath];
    }
}

-(void)handleOtherProjectFile:(NSString *)fileName filePath:(NSString *)filePath
{
    NSArray *musicExtensions = @[@"mp3",@"m4a"];
    BOOL isMusic = [musicExtensions containsObject:[[fileName pathExtension] lowercaseString]];
    if (isMusic) {
        NSLog(@"是音乐文件");
        NSString *musicFolder = MusicFolder;
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        if ([fileManager fileExistsAtPath:[musicFolder stringByAppendingPathComponent:[filePath lastPathComponent]]]) {
            NSLog(@"文件已存在，不用再次保存了");
            [self playMusic:fileName filePath:[musicFolder stringByAppendingPathComponent:[filePath lastPathComponent]]];
            NSError *removeFileError = nil;
            if ([fileManager removeItemAtPath:filePath error:&removeFileError]) {
                NSLog(@"源文件删除成功");
            }
            else
            {
                NSLog(@"源文件删除失败：%@",removeFileError);
            }
            return;
        }
        
        if ([fileManager fileExistsAtPath:musicFolder]) {
            NSError * error = nil;
            if ([fileManager copyItemAtPath:filePath toPath:[musicFolder stringByAppendingPathComponent:[filePath lastPathComponent]] error:&error]) {
                NSLog(@"文件已移动至Library目录下成功");
                
                
                
                [self playMusic:fileName filePath:[musicFolder stringByAppendingPathComponent:[filePath lastPathComponent]]];
                
                
                [self createMusicListFile:[filePath lastPathComponent]];
                NSError *removeFileError = nil;
                if ([fileManager removeItemAtPath:filePath error:&removeFileError]) {
                    NSLog(@"源文件删除成功");
                }
                else
                {
                    NSLog(@"源文件删除失败：%@",removeFileError);
                }
            }
            else
            {
                NSLog(@"文件移动至Library目录下失败:%@",error);
            }
        }
        else
        {
            
            static NSInteger count = 0;
            if ([fileManager createDirectoryAtPath:musicFolder withIntermediateDirectories:YES attributes:nil error:nil]) {
                NSLog(@"文件夹创建成功");
                sleep(1.0);
                NSError * error = nil;
                if ([fileManager copyItemAtPath:filePath toPath:[musicFolder stringByAppendingPathComponent:[filePath lastPathComponent]] error:&error]) {
                    NSLog(@"文件已移动至Library目录下成功");
                    
                    [self playMusic:fileName filePath:[musicFolder stringByAppendingPathComponent:[filePath lastPathComponent]]];
                    
                    [self createMusicListFile:[filePath lastPathComponent]];
                    NSError *removeFileError = nil;
                    if ([fileManager removeItemAtPath:filePath error:&removeFileError]) {
                        NSLog(@"源文件删除成功");
                    }
                    else
                    {
                        NSLog(@"源文件删除失败：%@",removeFileError);
                    }
                }
                else
                {
                    NSLog(@"文件移动至Library目录下失败:%@",error);
                    if (count < 10) {
                        sleep(count * 1.0);
                        [self handleOtherProjectFile:fileName filePath:filePath];
                        count++;
                        NSLog(@"count = %ld",count);
                    }
                }
            }
            else
            {
                NSLog(@"文件夹创建失败");
            }
        }
    }
}

-(void)playMusic:(NSString *)fileName filePath:(NSString *)filePath
{
    
    
    
    
    
    AlbumModel *model = [[AlbumModel alloc] init];
    model.songName = [fileName stringByDeletingPathExtension];
    model.songExtensionName = [fileName pathExtension];
    model.songPath = [LibraryFolder stringByAppendingPathComponent:[@"OpenOtherFile/MusicFolder" stringByAppendingPathComponent:fileName]];
    model.type = WorksSourceTypeLocal;
    model.downloadFolderName = @"OpenOtherFile/MusicFolder";
    model.albumName = [fileName stringByDeletingPathExtension];
    model.singerName = [fileName stringByDeletingPathExtension];
    model.albumImage = @"album_0.jpg";
    model.singerImage = @"0.jpg";
    model.videoURL = @"";
    
    
    
    MusicPlayingViewController *musicPlayingVC = [MusicPlayingViewController sharedInstance];
    musicPlayingVC.navigationItem.title = model.songName;
    musicPlayingVC.playListModels = @[model];
    musicPlayingVC.currentPlayIndex = 0;
    [musicPlayingVC play:model];
    
    if ([[self getCurrentVC] isKindOfClass:[MusicPlayingViewController class]]) {
        NSLog(@"正在播放界面，无需再次进入");
    }
    else
    {
        [self.navigationController pushViewController:musicPlayingVC animated:YES];
    }
    
    
//    NSMutableArray *localSongs = [NSMutableArray new];
//    for (WorksPathModel *object in self.dataSource) {
//        if ([object.worksName isEqualToString:model.songName]) {
//            [localSongs addObjectsFromArray:object.localSongs];
//            break;
//        }
//    }
//    
//    
//    for (AlbumModel *object in localSongs) {
//        if ([object.songName isEqualToString:model.songName]) {
//            NSLog(@"播放列表里面已经有该文件了");
//            return;
//        }
//    }
//    
//    
//    [localSongs addObject:model];
//    
////    NSArray *newSongArr = [AlbumModel mj_objectArrayWithKeyValuesArray:localSongs];
////    NSMutableDictionary *userInfo = [NSMutableDictionary new];
////    userInfo[@"folderName"] = model.songName;
////    userInfo[@"folders"] = newSongArr;
////    [[NSNotificationCenter defaultCenter] postNotificationName:@"addSongList" object:nil userInfo:userInfo];
//    
//    
//    
//    WorksPathModel *worksPathModel = [[WorksPathModel alloc] init];
//    worksPathModel.worksName = model.songName;
//    worksPathModel.worksPath = @"";
//    worksPathModel.type = WorksSourceTypeLocal;
//    worksPathModel.localSongs = localSongs;
//
//    worksPathModel.downloadSongFileFolderPath = model.downloadFolderName;
//
//    NSDictionary *dic = [worksPathModel.localSongs firstObject];
//    NSLog(@"downloadSongFileFolderPath = %@",[dic[@"songPath"] stringByDeletingLastPathComponent]);
//    NSLog(@"worksPathModel.downloadSongFileFolderPath = %@",worksPathModel.downloadSongFileFolderPath);
//    [self.dataSource addObject:worksPathModel];
//
//
//    for (WorksPathModel *object in self.dataSource) {
//        if ([object.worksName isEqualToString:model.songName]) {
//            [self.dataSource removeObject:object];
//            break;
//        }
//    }
//    [self.dataSource addObject:worksPathModel];
//    [self.tableView reloadData];
    
    
    
    
    
    
}

-(UIViewController *)getCurrentVC
{
    UIViewController* currentViewController = [self getRootViewController];
    BOOL runLoopFind = YES;
    while (runLoopFind)
    {
        if (currentViewController.presentedViewController)
        {
            currentViewController = currentViewController.presentedViewController;
        }
        else if ([currentViewController isKindOfClass:[UINavigationController class]])
        {
            UINavigationController* navigationController = (UINavigationController* )currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];
        }
        else if ([currentViewController isKindOfClass:[UITabBarController class]])
        {
            UITabBarController* tabBarController = (UITabBarController* )currentViewController;
            currentViewController = tabBarController.selectedViewController;
        }
        else
        {
            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
            if (childViewControllerCount > 0)
            {
                currentViewController = currentViewController.childViewControllers.lastObject;
                return currentViewController;
            }
            else
            {
                return currentViewController;
            }
        }
    }
    return currentViewController;
}

- (UIViewController *)getRootViewController{
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    return window.rootViewController;
}

-(void)createMusicListFile:(NSString *)fileName
{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:OtherOpenFilePath]) {
        NSMutableArray *otherOpenFileArr = [NSMutableArray arrayWithContentsOfFile:OtherOpenFilePath];
        //        NSMutableArray *otherMusicArr = [NSMutableArray new];
        NSMutableArray *otherMusics = [NSMutableArray new];
        NSMutableDictionary *otherMusicDic = [NSMutableDictionary new];
        
        for (id object in otherOpenFileArr) {
            if ([object isKindOfClass:[NSDictionary class]]) {
                NSDictionary *musicDic = (NSDictionary *)object;
                if ([musicDic[@"folderName"] isEqualToString:@"music"]) {
                    otherMusicDic = [NSMutableDictionary dictionaryWithDictionary:musicDic];
                    NSArray *otherMusic = musicDic[@"folders"];
                    [otherMusics addObjectsFromArray:otherMusic];
                    NSLog(@"已经打开过其他音乐文件！");
                    break;
                }
                
            }
        }
        
        
        
        for (NSDictionary *musicDic in otherMusics) {
            if ([musicDic[@"musicFullName"] isEqualToString:fileName]) {
                NSLog(@"文件记录已存在，无需再次保存");
                return;
            }
        }
        
        for (id object in otherOpenFileArr) {
            if ([object isKindOfClass:[NSDictionary class]]) {
                NSDictionary *musicDic = (NSDictionary *)object;
                if ([musicDic[@"folderName"] isEqualToString:@"music"]) {
                    [otherOpenFileArr removeObject:musicDic];
                    NSLog(@"移除文件");
                    break;
                }
                
            }
        }
        
        NSMutableDictionary *musicDic = [NSMutableDictionary new];
        musicDic[@"musicName"] = [fileName stringByDeletingPathExtension];
        musicDic[@"extensionName"] = [fileName pathExtension];
        musicDic[@"musicFullName"] = fileName;
        musicDic[@"songPath"] = [@"OpenOtherFile/MusicFolder" stringByAppendingPathComponent:fileName];
        musicDic[@"albumImage"] = @"album_0.jpg";
        musicDic[@"albumName"] = [fileName stringByDeletingPathExtension];
        musicDic[@"singerImage"] = @"0.jpg";
        musicDic[@"singerName"] = [fileName stringByDeletingPathExtension];
        musicDic[@"videoURL"] = fileName;
        musicDic[@"writer"] = [fileName stringByDeletingPathExtension];
        [otherMusics addObject:musicDic];
        
        otherMusicDic[@"folders"] = otherMusics;
        otherMusicDic[@"folderName"] = @"music";
        [otherOpenFileArr addObject:otherMusicDic];
        if ([otherOpenFileArr writeToFile:OtherOpenFilePath atomically:YES]) {
            NSLog(@"文件保存成功");
        }
        else
        {
            NSLog(@"文件保存失败");
        }
        
    }
    else
    {
        NSMutableArray *otherOpenFileArr = [NSMutableArray new];
        NSMutableDictionary *otherMusicDic = [NSMutableDictionary new];
        otherMusicDic[@"folderName"] = @"music";
        NSMutableArray *musicArr = [NSMutableArray new];
        NSMutableDictionary *musicDic = [NSMutableDictionary new];
        musicDic[@"musicName"] = [fileName stringByDeletingPathExtension];
        musicDic[@"extensionName"] = [fileName pathExtension];
        musicDic[@"musicFullName"] = fileName;
        musicDic[@"songPath"] = [@"OpenOtherFile/MusicFolder" stringByAppendingPathComponent:fileName];
        musicDic[@"albumImage"] = @"album_0.jpg";
        musicDic[@"albumName"] = [fileName stringByDeletingPathExtension];
        musicDic[@"singerImage"] = @"0.jpg";
        musicDic[@"singerName"] = [fileName stringByDeletingPathExtension];
        musicDic[@"videoURL"] = fileName;
        musicDic[@"writer"] = [fileName stringByDeletingPathExtension];
        [musicArr addObject:musicDic];
        otherMusicDic[@"folders"] = musicArr;
        
        [otherOpenFileArr addObject:otherMusicDic];
        if ([otherOpenFileArr writeToFile:OtherOpenFilePath atomically:YES]) {
            NSLog(@"文件保存成功");
        }
        else
        {
            NSLog(@"文件保存失败");
        }
    }
    
    
    
    
    
    
    
    
    
    
    
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    if ([fileManager fileExistsAtPath:OtherOpenFilePath]) {
//        NSMutableArray *otherOpenFileArr = [NSMutableArray arrayWithContentsOfFile:OtherOpenFilePath];
////        NSMutableArray *otherMusicArr = [NSMutableArray new];
//        NSMutableArray *otherMusics = [NSMutableArray new];
//        NSMutableDictionary *otherMusicDic = [NSMutableDictionary new];
//
//        for (id object in otherOpenFileArr) {
//            if ([object isKindOfClass:[NSDictionary class]]) {
//                NSDictionary *musicDic = (NSDictionary *)object;
//                if ([musicDic[@"folderName"] isEqualToString:@"music"]) {
//                    otherMusicDic = [NSMutableDictionary dictionaryWithDictionary:musicDic];
//                    NSArray *otherMusic = musicDic[@"folders"];
//                    [otherMusics addObjectsFromArray:otherMusic];
//                    NSLog(@"已经打开过其他音乐文件！");
//                    break;
//                }
//
//            }
//        }
//
//
//
//        for (NSDictionary *musicDic in otherMusics) {
//            if ([musicDic[@"musicFullName"] isEqualToString:fileName]) {
//                NSLog(@"文件记录已存在，无需再次保存");
//                return;
//            }
//        }
//
//        for (id object in otherOpenFileArr) {
//            if ([object isKindOfClass:[NSDictionary class]]) {
//                NSDictionary *musicDic = (NSDictionary *)object;
//                if ([musicDic[@"folderName"] isEqualToString:@"music"]) {
//                    [otherOpenFileArr removeObject:musicDic];
//                    NSLog(@"移除文件");
//                    break;
//                }
//
//            }
//        }
//
//        NSMutableDictionary *musicDic = [NSMutableDictionary new];
//        musicDic[@"musicName"] = [fileName stringByDeletingPathExtension];
//        musicDic[@"extensionName"] = [fileName pathExtension];
//        musicDic[@"musicFullName"] = fileName;
//        musicDic[@"songPath"] = [@"OpenOtherFile/MusicFolder" stringByAppendingPathComponent:fileName];
//        musicDic[@"albumImage"] = @"album_0.jpg";
//        musicDic[@"albumName"] = [fileName stringByDeletingPathExtension];
//        musicDic[@"singerImage"] = @"0.jpg";
//        musicDic[@"singerName"] = [fileName stringByDeletingPathExtension];
//        musicDic[@"videoURL"] = fileName;
//        musicDic[@"writer"] = [fileName stringByDeletingPathExtension];
//        [otherMusics addObject:musicDic];
//
//        otherMusicDic[@"folders"] = otherMusics;
//        otherMusicDic[@"folderName"] = @"music";
//        [otherOpenFileArr addObject:otherMusicDic];
//        if ([otherOpenFileArr writeToFile:OtherOpenFilePath atomically:YES]) {
//            NSLog(@"文件保存成功");
//        }
//        else
//        {
//            NSLog(@"文件保存失败");
//        }
//
//    }
//    else
//    {
//        NSMutableArray *otherOpenFileArr = [NSMutableArray new];
//        NSMutableDictionary *otherMusicDic = [NSMutableDictionary new];
//        otherMusicDic[@"folderName"] = @"music";
//        NSMutableArray *musicArr = [NSMutableArray new];
//        NSMutableDictionary *musicDic = [NSMutableDictionary new];
//        musicDic[@"musicName"] = [fileName stringByDeletingPathExtension];
//        musicDic[@"extensionName"] = [fileName pathExtension];
//        musicDic[@"musicFullName"] = fileName;
//        musicDic[@"songPath"] = [@"OpenOtherFile/MusicFolder" stringByAppendingPathComponent:fileName];
//        musicDic[@"albumImage"] = @"album_0.jpg";
//        musicDic[@"albumName"] = [fileName stringByDeletingPathExtension];
//        musicDic[@"singerImage"] = @"0.jpg";
//        musicDic[@"singerName"] = [fileName stringByDeletingPathExtension];
//        musicDic[@"videoURL"] = fileName;
//        musicDic[@"writer"] = [fileName stringByDeletingPathExtension];
//        [musicArr addObject:musicDic];
//        otherMusicDic[@"folders"] = musicArr;
//
//        [otherOpenFileArr addObject:otherMusicDic];
//        if ([otherOpenFileArr writeToFile:OtherOpenFilePath atomically:YES]) {
//            NSLog(@"文件保存成功");
//        }
//        else
//        {
//            NSLog(@"文件保存失败");
//        }
//    }
}



#pragma mark -------------- zip解压缩 --------------
- (void)unZip:(NSString *)zipFilePath{

    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:zipFilePath andPassword:nil destinationPath:destPath];
    
}

- (NSString *)applicationDocumentsDirectory
{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    basePath = [basePath stringByAppendingPathComponent:@"OpenOtherFile"];
    return basePath;
}


- (void)unArchive: (NSString *)filePath andPassword:(NSString*)password destinationPath:(NSString *)destPath{
    NSAssert(filePath, @"can't find filePath");
    UNZipRARService *unarchive = [[UNZipRARService alloc]initWithPath:filePath];
    if (password != nil && password.length > 0) {
        unarchive.password = password;
    }
    __weak typeof(self) weakSelf = self;
    if (destPath != nil)
        
        unarchive.destinationPath = destPath;//(Optional). If it is not given, then the file is unarchived in the same location of its archive/file.
    
    unarchive.downLoadFilePathBlock = ^(NSString *filePath){
        //zip取
        NSLog(@"%@",filePath);
        
        [self removeItemWithFolder:weakSelf.currentOpenFileTempPath];
        
        OtherFileFolderViewController *otherFileFolderVC = [[OtherFileFolderViewController alloc] init];
        otherFileFolderVC.navigationItem.title = @"OpenOtherFile";
        
        otherFileFolderVC.folderPath = @"OpenOtherFile";
        BackItemView
        otherFileFolderVC.folderName = @"OpenOtherFile";
        [weakSelf.navigationController pushViewController:otherFileFolderVC animated:YES];
        [SVProgressHUD dismiss];
        
    };
    unarchive.completionBlock = ^(NSArray *filePaths){
        //rar取
        NSLog(@"For Archive : %@",filePath);
        for (NSString *filename in filePaths) {
            NSLog(@"Extracted Filepath: %@", filename);
        }
    };
    unarchive.failureBlock = ^(){
        NSLog(@"Cannot be unarchived");
    };
    [unarchive decompress];
}

-(void)removeItemWithFolder:(NSString *)folder
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager removeItemAtPath:folder error:nil]) {
        NSLog(@"文件删除成功");
    }
    else
    {
        NSLog(@"文件删除失败");
    }
}

-(void)addSongList:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSLog(@"userInfo = %@",userInfo);
    
    WorksPathModel *model = [[WorksPathModel alloc] init];
    model.worksName = userInfo[@"folderName"];
    model.worksPath = @"";
    model.type = WorksSourceTypeLocal;
    model.downloadSongFileFolderPath = userInfo[@"downloadSongFileFolderPath"];
    model.localSongs = userInfo[@"folders"];
    [self.dataSource addObject:model];
    
    NSLog(@"[NSThread currentThread] = %@",[NSThread currentThread]);
    
    [self.tableView reloadData];
}

-(void)removeItemMethod:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSLog(@"userInfo = %@",userInfo);
    NSString *songName = userInfo[@"songName"];
    
    
    NSMutableArray *songArr = [NSMutableArray new];
    [songArr addObjectsFromArray:self.dataSource];
    
    for (WorksPathModel *model in songArr) {
        if ([model.worksName isEqualToString:songName]) {
            [self.dataSource removeObject:model];
        }
    }
    
    
//    NSMutableArray *songListArr = [NSMutableArray arrayWithArray:self.dataSource];
//    for (WorksPathModel *model in songListArr) {
//        if ([model.worksName isEqualToString:songName]) {
//            [songListArr removeObject:model];
//        }
//    }
//    self.dataSource = [NSMutableArray arrayWithArray:songListArr];
    [self.tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"万能播放器";
    
    //用其他软件打开 传进来的文件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileNotification:) name:OpenFileNotification object:nil];
    
    
    //当 用其他软件打开 的文件为音频文件时，将音频文件添加到列表中
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addSongList:) name:@"addSongList" object:nil];
    
    //删除 用其他软件打开 传进来的文件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeItemMethod:) name:@"removeItemNotification" object:nil];
    
    
    PlayerBottomView *bottomView = [PlayerBottomView shareInstance];
    UIWindow *windows = [[UIApplication sharedApplication] keyWindow];
    [windows addSubview:bottomView];
    bottomView.frame = CGRectMake(0, kScreenHeight - 100, kScreenWidth, 100);
    bottomView.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakSelf = self;
    bottomView.ClickViewBlock = ^(PlayerBottomView * _Nonnull bottomView) {
        NSLog(@"相应点击事件");
        MusicPlayingViewController *playingVC = [MusicPlayingViewController sharedInstance];
        if ([playingVC isPlaying]) {
            NSLog(@"正在播放音乐");
        }
        else
        {
            NSLog(@"未处于播放状态");
            playingVC.currentPlayIndex = bottomView.currentPlayingIndex;
            playingVC.playListModels = bottomView.playingModelList;
            [playingVC play:bottomView.model];
            [playingVC playingToLength:bottomView.model.currentDuration];
        }
        [weakSelf.navigationController pushViewController:playingVC animated:YES];
    };
    NSDictionary *currentPlayingMusicDic = [NSDictionary dictionaryWithContentsOfFile:CurrentPlayingMusicPath];
    if (currentPlayingMusicDic) {
//        NSLog(@"currentPlayingMusicDic = %@",currentPlayingMusicDic);
        CurrentPlayingMusicModel *currentPlayingModel = [CurrentPlayingMusicModel mj_objectWithKeyValues:currentPlayingMusicDic];
        bottomView.currentPlayingIndex = currentPlayingModel.playingIndex;
        bottomView.playingModelList = [AlbumModel mj_objectArrayWithKeyValuesArray:currentPlayingModel.playingList];
        bottomView.model = bottomView.playingModelList[bottomView.currentPlayingIndex];
        bottomView.model.currentDuration = currentPlayingModel.currentDuration;
        bottomView.progress = currentPlayingModel.progress;
    }
    else
    {
        NSLog(@"当前无正在播放的歌曲");
    }
    
    
    
//    _player = [[VLCMediaPlayer alloc] init];
//    NSString *videoURLString = @"http://124.193.120.172/hc.yinyuetai.com/uploads/videos/common/D355015CD299115D47C9B8716045D43A.mp4";
//    videoURLString = [videoURLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    VLCMedia *media = [VLCMedia mediaWithURL:[NSURL URLWithString:videoURLString]];
////    VLCMedia *media = [VLCMedia mediaWithURL:[NSURL URLWithString:@"http://124.193.120.172/hc.yinyuetai.com/uploads/videos/common/D355015CD299115D47C9B8716045D43A.mp4"]];
//    _player.drawable = self.view;
//    _player.media = media;
//    [_player play];
//    return;
    // Do any additional setup after loading the view.
    
    NSArray *pathArr = [[NSBundle mainBundle] pathsForResourcesOfType:@"plist" inDirectory:@"NetSource"];
//    NSLog(@"pathArr = %@",pathArr);
    NSMutableArray *netFileList = [NSMutableArray new];
    for (NSString *path in pathArr) {
        NSString *name = [[path stringByDeletingPathExtension] lastPathComponent];
        NSString *fullName = [path lastPathComponent];
        NSMutableDictionary *pathNameDic = [NSMutableDictionary new];
        pathNameDic[@"worksName"] = name;
        pathNameDic[@"worksPath"] = fullName;
        pathNameDic[@"sourceType"] = [NSNumber numberWithInteger:WorksSourceTypeNet];
        [netFileList addObject:pathNameDic];
    }
    
    
    
    NSArray *sortArr = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"worksName" ascending:YES]];
    [netFileList sortUsingDescriptors:sortArr];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:OtherOpenFilePath]) {
        NSArray *localSongArr = [NSArray arrayWithContentsOfFile:OtherOpenFilePath];
        for (NSDictionary *dic in localSongArr) {
            if ([dic isKindOfClass:[NSDictionary class]]) {
                WorksPathModel *model = [[WorksPathModel alloc] init];
                model.worksName = dic[@"folderName"];
                model.worksPath = @"";
                model.type = WorksSourceTypeLocal;
                model.localSongs = dic[@"folders"];
                if (model.localSongs.count > 0) {
                    NSDictionary *dic = [model.localSongs firstObject];
                    model.downloadSongFileFolderPath = [dic[@"songPath"] stringByDeletingLastPathComponent];
                    
                }
                [self.dataSource addObject:model];
            }
            
        }
    }
    
   
    
    NSMutableArray *worksTempList = [NSMutableArray new];
//    NSDictionary *localDic = @{@"worksName":@"知否？知否？应是绿肥红瘦(懒人听书)_本地",@"worksPath":@"source/知否？知否？应是绿肥红瘦(懒人听书)",@"sourceType":[NSNumber numberWithInteger:WorksSourceTypeLocal]};
//    [worksTempList addObject:localDic];
    [worksTempList addObjectsFromArray:netFileList];
    NSArray *worksList = [NSArray arrayWithArray:worksTempList];
    
    for (NSDictionary *dic in worksList) {
        WorksPathModel *model = [[WorksPathModel alloc] init];
        model.worksName = dic[@"worksName"];
        model.worksPath = dic[@"worksPath"];
        model.type = [dic[@"sourceType"] integerValue];
        model.downloadSongFileFolderPath = [NSString stringWithFormat:@"%@/song",model.worksName];
        [self.dataSource addObject:model];
    }
    [self initTableView];
}

-(void)initTableView
{
    PlayerBottomView *bottomView = [PlayerBottomView shareInstance];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - CGRectGetHeight(bottomView.frame)) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[WorksListTableViewCell class] forCellReuseIdentifier:@"WorksListTableViewCell"];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorksListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorksListTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WorksPathModel *model = self.dataSource[indexPath.row];
    cell.model = model;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MusicListViewController *musicListVC = [[MusicListViewController alloc] init];
    WorksPathModel *model = self.dataSource[indexPath.row];
    musicListVC.navigationItem.title = model.worksName;
    musicListVC.worksPathModel = model;
    BackItemView
    [self.navigationController pushViewController:musicListVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

-(void)createTeGongHuangFeiChuQiaoZhuan_YiNiDuoDuoSource
{
    
    NSMutableArray *musicArr = [NSMutableArray new];
    
    for (NSInteger i = 1; i < 51; ++i) {
//
        
        
        NSMutableDictionary *musicDic = [NSMutableDictionary new];
        NSString *urlString = [NSString stringWithFormat:@"https://pp.ting55.com/201905060258/9daf9981a03bcd39052de72c9ef9026c/2018/08/12706/%ld.mp3",i];
        https://pp.ting55.com/201905060308/809ae3b08adb64f09e2918454477bfcb/2018/08/12706/50.mp3
        musicDic[@"musicName"] = [NSString stringWithFormat:@"妙偶天成_%.3ld",i];
        musicDic[@"extensionName"] = [urlString pathExtension];
        musicDic[@"musicFullName"] = [NSString stringWithFormat:@"%@.%@",musicDic[@"musicName"],musicDic[@"extensionName"]];
        musicDic[@"songPath"] = urlString;
        musicDic[@"albumName"] = @"妙偶天成_一月";
        musicDic[@"singerName"] = @"一月";
        musicDic[@"writer"] = @"冬天的柳叶";
        musicDic[@"videoURL"] = @"";
        musicDic[@"albumImage"] = [NSString stringWithFormat:@"album_%ld.jpg",(i % 4)];
        musicDic[@"singerImage"] = [NSString stringWithFormat:@"%ld.jpg",(i % 4)];
        [musicArr addObject:musicDic];
        
        
    }
    
    NSString *bj_xr = DownloadFilePath(@"妙偶天成_一月.plist");
    NSLog(@"cqz = %@",bj_xr);
    [musicArr writeToFile:bj_xr atomically:YES];



//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"花千骨_筱梦、布兰德.plist" ofType:@""];
//    NSArray *sourceArr = [NSArray arrayWithContentsOfFile:filePath];
//    NSMutableArray *musicArr = [NSMutableArray new];
//
//    for (NSInteger i = 1; i < sourceArr.count + 1; i++) {
//        NSMutableDictionary *musicDic = [NSMutableDictionary dictionaryWithDictionary:sourceArr[i - 1]];
//        musicDic[@"musicName"] = [NSString stringWithFormat:@"花千骨_%.3ld",i];
//        musicDic[@"musicFullName"] = [NSString stringWithFormat:@"%@.%@",musicDic[@"musicName"],musicDic[@"extensionName"]];
//        [musicArr addObject:musicDic];
//    }
//    NSLog(@"musicArr = %@",musicArr);
//    NSString *bj_xr = DownloadFilePath(@"cqz.plist");
//    NSLog(@"cqz = %@",bj_xr);
//    [musicArr writeToFile:bj_xr atomically:YES];






//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"错妃诱情_陌上轻寒_temp.plist" ofType:@""];
//    NSArray *sourceArr = [NSArray arrayWithContentsOfFile:filePath];
//    NSMutableArray *musicArr = [NSMutableArray new];
//
//    for (NSInteger i = 1; i < sourceArr.count + 1; i++) {
//        NSMutableDictionary *musicDic = [NSMutableDictionary new];
//        NSString *urlString = sourceArr[i - 1];
//        musicDic[@"musicName"] = [NSString stringWithFormat:@"错妃诱情_%.3ld",i];
//        musicDic[@"extensionName"] = [urlString pathExtension];
//        musicDic[@"musicFullName"] = [NSString stringWithFormat:@"%@.%@",musicDic[@"musicName"],musicDic[@"extensionName"]];
//        musicDic[@"songPath"] = urlString;
//        musicDic[@"albumName"] = @"错妃诱情_陌上轻寒";
//        musicDic[@"singerName"] = @"陌上轻寒";
//        musicDic[@"writer"] = @"月出云";
//        musicDic[@"albumImage"] = [NSString stringWithFormat:@"album_%ld.jpg",(i % 4)];
//        musicDic[@"singerImage"] = [NSString stringWithFormat:@"%ld.jpg",(i % 4)];
//        [musicArr addObject:musicDic];
//    }
//    NSLog(@"musicArr = %@",musicArr);
//    NSString *bj_xr = DownloadFilePath(@"错妃诱情_陌上轻寒.plist");
//    NSLog(@"cqz = %@",bj_xr);
//    [musicArr writeToFile:bj_xr atomically:YES];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
